package net.wzw.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import net.wzw.redis.RedisLock;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @className: RedisTemplateConfig
 * @author: wzw
 * @date: 2025-04-09 9:20
 * @Version: 1.0
 * @description: redis 模板配置
 */
@Data
@Configuration
@ConditionalOnClass(RedisConnectionFactory.class)
public class RedisTemplateConfig {

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private String redisPort;

    @Value("${spring.redis.password}")
    private String redisPwd;

    @Value("${spring.redis.timeout}")
    private int redisTimeout;

    /**
     * 实例化 redisTemplate
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    @ConditionalOnClass(RedisConnectionFactory.class)
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

        // 实例化 RedisTemplate
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        // 设置连接
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        // 配置 Redis 序列化规则
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        // 设置 key-value 序列化规则
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

        // 设置 hash-value 序列化规则
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);

        return redisTemplate;
    }

    /**
     * 实例化 redissonClient
     * @return
     */
    @Bean
    @ConditionalOnClass(RedissonClient.class)
    public RedissonClient redissonClient(){
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://" + redisHost + ":" + redisPort)
                .setPassword(redisPwd)
                // 设置连接池最小空闲大小
                .setConnectionMinimumIdleSize(10)
                // 设置连接池大小
                .setConnectionPoolSize(100)
                // 设置空闲链接超时时间
                .setIdleConnectionTimeout(600000)
                .setSubscriptionConnectionMinimumIdleSize(10)
                .setSubscriptionConnectionPoolSize(100)
                // 设置超时时间
                .setTimeout(redisTimeout);

        config.setCodec(new StringCodec());
        // 设置线程数
        config.setThreads(5);
        // 设置 netty 线程数
        config.setNettyThreads(5);
        // 实例化 redisson 客户端
        RedissonClient client = Redisson.create(config);

        return client;
    }

    /**
     * 实例化分布式锁
     * @param redissonClient
     * @return
     */
    @Bean
    public RedisLock redisLock(RedissonClient redissonClient) {
        return new RedisLock(redissonClient);
    }
}
